<template>
  <div class="news">
    <nav class="news-list">
      <ul>
        <li v-for="n in newsList" :key="n.id">
          <button @click="redirectBtn(n)">{{ n.id }}跳转</button>
          <!-- query的方式 -->
          <!-- <RouterLink :to="{ path: '/news/detail', query: { id: n.id, name: n.name, content: n.content } }"> -->
          <RouterLink replace :to="{ name: 'xiangXi', query: { id: n.id, name: n.name, content: n.content } }">

            <!-- params的方式,只能使用name -->
            <!-- <RouterLink :to="`/news/detail/${n.id}/${n.name}/${n.content}`"> -->
            <!-- <RouterLink :to="{ name: 'xiangXi', params: { id: n.id, name: n.name, content: n.content } }"> -->
            {{ n.name }}
          </RouterLink>
        </li>
      </ul>
    </nav>
    <div class="news-detail">
      <RouterView />
    </div>
  </div>
</template>
  
<script setup lang="ts" name="News">
import { reactive } from 'vue'
import { RouterView, RouterLink } from 'vue-router';
import { useRouter } from 'vue-router'
const router = useRouter()     //拿到路由器

let newsList = reactive([
  { id: '01', name: 'name01', content: 'content01' },
  { id: '02', name: 'name02', content: 'content02' },
  { id: '03', name: 'name03', content: 'content03' },
  { id: '04', name: 'name04', content: 'content04' }
])

function redirectBtn(myNew: any) {
  router.push({ name: 'xiangXi', query: { id: myNew.id, name: myNew.name, content: myNew.content } })  //跳转，并有历史记录，可以后退
  //router.replace({ name: 'xiangXi', query: { id: myNew.id, name: myNew.name, content: myNew.content } }) //跳转，无历史记录，不可以后退
}

</script>
  
<style scoped>
/* 新闻 */
.news {
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  height: 100%;
}

.news ul {
  margin-top: 30px;
  list-style: none;
  padding-left: 10px;
}

.news li>a {
  font-size: 18px;
  line-height: 40px;
  text-decoration: none;
  color: #64967E;
  text-shadow: 0 0 1px rgb(0, 84, 0);
}

.news-content {
  width: 70%;
  height: 90%;
  border: 1px solid;
  margin-top: 20px;
  border-radius: 10px;
}
</style>